package com.liangzhong.model.match
{
	import com.common.utils.HashMap;
	import com.liangzhong.constants.ShareConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.ShareEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.MatchType;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;

	/**
	 *比赛数据管理 
	 * @author lake
	 * 
	 */	
	public class MatchDataManager
	{
		/**
		 * 字典 多个游戏
		 *是否主场作战 
		 */		
		public var isHome 		:Dictionary = new Dictionary();
		/**
		 * 字典 多个游戏
		 *是否游戏开始 
		 */		
		public var isGameStart :HashMap = new HashMap();
		/**
		 *游戏开始信息map 
		 */		
		public var gameStart 	:HashMap = new HashMap();
		/**
		 *是否录像 
		 */		
		public var isHistory	:Boolean = false;
		/**
		 *A 对球员id 包括替补 
		 */		
		//public var aPlayerIds	:Array;
		/**
		 *B 对球员id 包括替补 
		 */		
		//public var bPlayerIds	:Array;
		/**
		 *正在进行比赛类型 
		 */		
		public var matchType :int ;
		/**
		 *记录列表 
		 */		
		public var historyList :Array = [];
		
		/**
		 *缩小比赛数 
		 */		
		public static var SmallCount	:int = 0;
		/**
		 * 缩小条目列表
		 */		
		public static var SmallItemS	:Array = [];
		/**
		 * 材质资源位图
		 */		
		public static var sTextureBitmapDatas 	:Dictionary = new Dictionary();
		/**
		 * 位图资源
		 */		
		public static var mBitmapDataMaps 		:Dictionary = new Dictionary();
		/**
		 *背景位图 
		 */		
		public static var matchBgBitmap		:Bitmap;
		/**
		 *材质配置资源 （XML） 
		 */		
		public static var sTexturesConfig		:Dictionary = new Dictionary();
		/**
		 *背景位图映射
		 */		
		public static var matchBgBitmapDataMaps:Dictionary = new Dictionary();
		
		public function MatchDataManager()
		{
			mBitmapDataMaps["ROLE_HOME"] 		= new Dictionary();
			mBitmapDataMaps["KEEPER_HOME"] 	= new Dictionary();
			mBitmapDataMaps["KEEPER_AWAY"] 	= new Dictionary();
			mBitmapDataMaps["REFEREE"] 			= new Dictionary();
		}
		/**
		 *获取自己ids 
		 * @return 
		 * 
		 */		
		/*public function get selfIds():Array
		{
			if(isHome)
			{
				return aPlayerIds.slice(0,11);
			}
			else
			{
				return bPlayerIds.slice(0,11);
			}
		}*/
		/**
		 * 设置是否开始值 
		 * @param key
		 * @param value
		 * 
		 */		
		public function putIsGameStart(key :*,value:*):void
		{
			isGameStart.put(key,value);
		}
		/**
		 *获取值 
		 * @param key
		 * @return 
		 * 
		 */		
		public function getIsGameStartValue(key : *):*
		{
			return isGameStart.getValue(key);
		}
		/**
		 *获取长度 
		 * @return 
		 */		
		public function getIsGameStartLen():int
		{
			return isGameStart.size();
		}
		/**
		 * 设置开始值
		 * @param key
		 * @param value
		 * 
		 */		
		public function putGameStart(key :*,value:*):void
		{
			gameStart.put(key,value);
		}
		/**
		 *获取值 
		 * @param key
		 * @return 
		 * 
		 */		
		public function getGameStartValue(key : *):*
		{
			return gameStart.getValue(key);
		}
		/**
		 *删除值 
		 * @param key
		 */		
		public function removeIsGameStart(key : *):void
		{
			isGameStart.remove(key);
			if(isGameStart.size() == 0)//无比赛进行
			{
				var key :String;
				var bitmapData :BitmapData;
				for(key in sTextureBitmapDatas)
				{
					bitmapData = sTextureBitmapDatas[key];
					bitmapData.dispose();
					delete sTextureBitmapDatas[key];
				}
				var tempDic :Dictionary;
				for(key in mBitmapDataMaps)
				{
					tempDic = mBitmapDataMaps[key];
					for(var key2 :String in tempDic)
					{
						bitmapData = tempDic[key2];
						if(bitmapData)
						{
							bitmapData.dispose();
						}
						delete tempDic[key2];
					}
					//delete mBitmapDataMaps[key];
				}
				if(matchBgBitmap && matchBgBitmap.bitmapData)
				{
					matchBgBitmap.bitmapData.dispose();
					matchBgBitmap = null;
				}
				for(key in matchBgBitmapDataMaps)
				{
					bitmapData = matchBgBitmapDataMaps[key];
					if(bitmapData)
					{
						bitmapData.dispose();
					}
					delete matchBgBitmapDataMaps[key];
				}
			}
		}
		/**
		 *删除值 
		 * @param key
		 */		
		public function removeGameStart(key : *):void
		{
			gameStart.remove(key);
		}
		/**
		 * 参加比赛
		 * @param type 1友谊赛，2天梯赛 3 大奖赛 4 好友赛 5 巡回赛
		 * 
		 */		
		public function joinMatch(type :int):void
		{
			matchType = type;
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_DATA_CHANGE,{type:type,step:true});
		}
		/**
		 *开始好友赛 
		 * @param type
		 * @param uid
		 * 
		 */		
		public function startFriendMatch(uid :int):void
		{
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.FRIEND_DATA_CHANGE,{uid:uid});
		}
		/**
		 *比赛结果 
		 * @param goalCount 进球数
		 * 
		 */		
		public function matchResult(goalCount :int):void
		{
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_DATA_CHANGE,{type:matchType,num:goalCount});
		}
		/**
		 * 比赛结果 输赢
		 * @param value
		 * 
		 */		
		public function winOrLose(value :Boolean):void
		{
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_DATA_CHANGE,{type:matchType,win:value});
		}
		/**
		 *比赛结束 
		 * @param cid 俱乐部id
		 * @param result 结果 
		 * 1.	胜利
		   2.	失败
		   3.	打平
		 */		
		public function matchEnd(cid :int,result:int,goalNum :int,opGoalNum:int):void
		{
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MATCH_END,{cid:cid,result:result,sNum:goalNum,oNum:opGoalNum,type:matchType});
			// 是否分享 
			if(result == 1 && (goalNum>=5))
			{
				//share
				var shareParam :Object = {};
				shareParam.type = getShareType(matchType);
				shareParam.score= goalNum + ":" + opGoalNum;
				if(matchType == MatchType.FRIENDMATCH || matchType == MatchType.TOURMATCH)
				{
//					shareParam.name = AppData.getInstance().stadiumDataManager.opponentMatchInfo.name;
				}
				AppEventDispatcher.getInstance().dispatchEvent(new ShareEvent(ShareEvent.SHARE_SAY,shareParam));
			}
		}
		/**
		 *获取分享Type 
		 * @param mahType
		 * @return 
		 * 
		 */		
		private function getShareType(mahType :int):int
		{
			switch(mahType)
			{
				case MatchType.FRIENDSHIPMATCH:
				{
					return ShareConst.FriendShipMatch_Five_Goal_Win;
				}
				case MatchType.LADDYMATCH:
				{
					return ShareConst.LadderMatch_Five_Goal_Win;
				}
				case MatchType.FRIENDMATCH:
				{
					return ShareConst.FriendMatch_Five_Goal_Win;
				}
				case MatchType.TOURMATCH:
				{
					return ShareConst.TourMatch_Five_Goal_Win;
				}
			}
			return ShareConst.FriendShipMatch_Five_Goal_Win;
		}
	}
}